<?php
namespace app\admin\controller;
use app\parent\admin;
use think\Db;

class Rule extends admin
{

    public function system()
    {
        if($this->isPost()){
            $data = $this->input();
            $data['ewm_img'] = $data['yun_t_yuming'].'/'.$data['ewm_img'];
            //dump($data);exit;
            file_put_contents('./web/default/default.php',json_encode($data));
            $this->setLog('系统设置','修改了系统设置');
            msgText('修改成功');
        }

        $view['info'] = get_default();
        return view('system',$view);
    }

    /**
    * showdoc
    * @catalog /主后台页面/日志管理
    * @title 测试列表
    * @description 日志管理
    * @url /index.php/admin/rule/log
    * @method get
    * @return 
    * @remark 
    */
    public function log($table = 'admin_log')
    {
        if(!$this->isAjax()){
            return view();
        }
        
        $where = $this->whereSql([
            'like' => ['admin_log_admin_name','admin_log_admin_ip','admin_log_rank_name','admin_log_event','admin_log_content'],
            'time' => ['admin_log_times'],
        ]);

        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];

        $data = Db::name($table)
            ->where($where)
            ->limit($page,$pa['limit'])
            ->order('id desc')
            // ->fetchSql(true)
            ->select();
            
        $count = Db::name($table)
            ->where($where)
            ->count('id');

        return $this->ajaxReturn(0,'',$data,$count);
        
    }

    /**
    * showdoc
    * @catalog /主后台页面/导航管理
    * @title 测试列表
    * @description 导航管理
    * @url /index.php/admin/rule/nav
    * @method get
    * @return 
    * @remark 
    */
    public function nav()
    {
        if(!$this->isAjax()){
            return view();
        }
        $data = $this->returnNav($pid = 0, $arr = [], $str = '|--');
        return $this->ajaxReturn(0,'操作成功',$data);
        
    }

    public function returnNav($pid = 0, $arr = [], $str = '|--')
    {
        $info = Db::name('admin_nav')->where('admin_nav_pid',$pid)->order('admin_nav_sort asc')->field('*,FROM_UNIXTIME(admin_nav_times,"%Y-%m-%d %h:%i:%s") as admin_nav_times')->select();
        if($info){
            foreach($info as $k => $v){
                $v['admin_nav_title'] = $str.$v['admin_nav_title'];
                $arr[] = $v;
                $arr = $this->returnNav($v['id'], $arr, $str.'|--');
            }
            return $arr;
        }else{
            return $arr;
        }
    }

    /**
    * showdoc
    * @catalog /主后台页面/导航修改
    * @title 测试列表
    * @description 导航修改
    * @url /index.php/admin/rule/navAdd
    * @return 
    * @remark 
    */
    public function navAdd()
    {
        
        $viewArr['nav'] = $this->returnNav($pid = 0, $arr = [], $str = '|--');
        
        if (!$this->isPost()) {
            return view('nav_add',$viewArr);
        }

        $data = $this->unInput('id'); // 获取所有提交参数

        $data['admin_nav_sort'] = $data['admin_nav_sort'] ? $data['admin_nav_sort'] : Db::name('admin_nav')->max('admin_nav_sort') + 1;
        $data['admin_nav_times'] = time();
        $is = Db::name('admin_nav')->insert($data);

        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
        
    }

    /**
    * showdoc
    * @catalog /主后台页面/导航添加
    * @title 测试列表
    * @description 导航添加
    * @url /index.php/admin/rule/navUp
    * @return 
    * @remark 
    */
    public function navUp()
    {
        $id = $this->input('id',0); // 获取id
        $viewArr['nav'] = $this->returnNav($pid = 0, $arr = [], $str = '|--');
        
        // 修改提交
        if (!$this->isPost()) {

            $viewArr['info'] = Db::name('admin_nav')->where('id',$id)->find();
            return view('nav_up',$viewArr);
        }

        $data = $this->unInput('id'); // 获取所有提交参数
        $is = Db::name('admin_nav')->where('id',$id)->update($data);

        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
        
    }

    /**
    * showdoc
    * @catalog /主后台页面/导航删除
    * @title 测试列表
    * @description 导航删除
    * @url /index.php/admin/rule/navUp
    * @return 
    * @remark 
    */
    public function navDel()
    {
        $id = $this->input('id',0); // 获取id
        
        $is = Db::name('admin_nav')->where('id','in',$id)->delete();

        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
        
    }
    // 代理设置-应用设置-展示
    public function app($table = 'agency_system_app')
    {
        if(!$this->isAjax()){
            return view();
        }
        $where = $this->whereSql([
            'like' => ['agency_system_app_is_show'],
            'time' => ['agency_system_app_times'],
            'eq' => ['agency_system_app_wid'],
        ]);

        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];
        $data = Db::name($table)
            ->where($where)
            ->limit($page,$pa['limit'])
            ->order('id desc')
            // ->fetchSql(true)
            ->select();
        //分页计算条数
        $count = Db::name($table)->where($where)->count('id');
        return $this->ajaxReturn(0,'',$data,$count);
    }

    // 代理设置-应用设置-添加
    public function appAdd()
    {
        if(!$this->isAjax()){
            return view('appAdd');
        }
        $data = $this->unInput('id');
        $data['agency_system_app_times'] = time();
        $is = Db::name('agency_system_app')->insert($data);
        if($is){
            return $this->ajaxReturn(0,"操作成功");
        }else{
            return $this->ajaxReturn(1,"操作失败");
        }
    }
    // 代理设置-应用设置-修改
    public function appUp()
    {
        $id = $this->input('id',0);
        if(!$this->isPost()){
            $data['info'] = Db::name('agency_system_app')->where('id',$id)->find();
            return view('appUp',$data);
        }
        $data = $this->unInput('id');
        $is = Db::name('agency_system_app')->where('id',$id)->update($data);
        if($is){
            return $this->ajaxReturn(0,"操作成功");
        }else{
            return $this->ajaxReturn(1,"操作失败");
        }
    }
    // 代理设置-应用设置-删除
    public function appDel()
    {
        $id = $this->input('id',0);
        $is = Db::name('agency_system_app')->where('id','in',$id)->delete();
        if($is){
            return $this->ajaxReturn(0,"操作成功");
        }else{
            return $this->ajaxReturn(1,"操作失败");
        }
    }
    // 代理设置-系统设置-展示
    public function agency($table = "agency_system")
    {
        if(!$this->isAjax()){
            return view();
        }
        $where = $this->whereSql([
            'like' => ['agency_system_is_show'],
            'time' => ['agency_system_times'],
            'eq' => ['agency_system_pid'],
        ]);

        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];
        $data = Db::name($table)
            ->where($where)
            ->limit($page,$pa['limit'])
            ->order('id desc')
            // ->fetchSql(true)
            ->select();
        //分页计算条数
        $count = Db::name($table)->where($where)->count('id');
        return $this->ajaxReturn(0,'',$data,$count);
    }
    // 代理设置-系统设置-添加
    public function agencyAdd()
    {
        if(!$this->isAjax()){
            return view('agencyAdd');
        }
        $data = $this->unInput('id');
        $data['agency_system_times'] = time();
        $is = Db::name('agency_system')->insert($data);
        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
    }
    // 代理设置-系统设置-修改
    public function agencyUp()
    {
        $id = $this->input('id',0);
        if(!$this->isPost()){
            $data['info'] = Db::name('agency_system')->where('id',$id)->find();
            return view('agencyUp',$data);
        }
        $data = $this->unInput('id');
        $is = Db::name('agency_system')->where('id',$id)->update($data);
        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
    }
    // 代理设置-系统设置-删除
    public function agencyDel()
    {
        $id = $this->input('id',0);
        $is = Db::name('agency_system')->where('id','in',$id)->delete();
        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作成功');
        }
    }
}
